// NewsController.java
package com.feiyisys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyisys.common.PageResult;
import com.feiyisys.common.Result;
import com.feiyisys.entity.News;
import com.feiyisys.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 文化遗产相关新闻表 前端控制器
 * </p>
 *
 * @author wq
 * @since 2025-10-10
 */
@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 分页查询所有新闻
     */
    @GetMapping
    public Result<IPage<News>> getPage(@RequestParam(defaultValue = "1") Long current,
                                       @RequestParam(defaultValue = "10") Long size) {
        Page<News> page = new Page<>(current, size);
        IPage<News> result = newsService.page(page);
        return Result.success(result);  // 修改为返回 Result<IPage<News>>
    }
    @GetMapping("/published")
    public Result<IPage<News>> getPagepublished(@RequestParam(defaultValue = "1") Long current,
                                       @RequestParam(defaultValue = "10") Long size) {
        Page<News> page = new Page<>(current, size);
        QueryWrapper<News> queryWrapper = new QueryWrapper<News>().eq("is_published", 1);
        IPage<News> result = newsService.page(page, queryWrapper);
        return Result.success(result);
    }

    /**
     * 根据 ID 查询新闻
     */
    @GetMapping("/{id}")
    public Result<News> getNewsById(@PathVariable Long id) {
        News news = newsService.getById(id);
        return Result.success(news);
    }
    /**
     * 根据标题模糊查询新闻
     */
    @GetMapping("/searchbytitle")
    public Result<IPage<News>> searchByTitle(@RequestParam String title,
                                             @RequestParam(defaultValue = "1") Long current,
                                             @RequestParam(defaultValue = "10") Long size) {
        Page<News> page = new Page<>(current, size);
        IPage<News> result = newsService.page(page,
                new QueryWrapper<News>().like("title", title));
        return Result.success(result);  // 修改为返回 Result<IPage<News>>，与getPage方法保持一致
    }


    /**
     * 新增新闻
     */
    @PostMapping
    public Result<Boolean> createNews(@RequestBody News news) {
        boolean saved = newsService.save(news);
        if (saved) {
            return Result.success(saved);
        } else {
            return Result.fail("新闻新增失败");
        }
    }

    /**
     * 更新新闻
     */
    @PutMapping
    public Result<Boolean> updateNews(@RequestBody News news) {
        // 特别处理发布状态和发布时间的关系
        if (news.getIsPublished() != null && news.getIsPublished() == 0) {
            // 如果设置为未发布，强制清空发布时间
            news.setPublishTime(null);
        }
        boolean updated = newsService.updateById(news);
        if (updated) {
            return Result.success(updated);
        } else {
            return Result.fail("新闻更新失败");
        }
    }

    /**
     * 删除新闻
     */
    @
            DeleteMapping("/{id}")
    public Result<Boolean> deleteNews(@PathVariable Long id) {
        boolean removed = newsService.removeById(id);
        if (removed) {
            return Result.success(removed);
        } else {
            return Result.fail("新闻删除失败");
        }
    }

    /**
     * 增加新闻浏览次数（用户浏览时调用）
     */
    @PutMapping("/view/{id}")
    public Result<Boolean> increaseViewCount(@PathVariable Long id) {
        News news = newsService.getById(id);
        if (news != null) {
            // 增加浏览次数
            news.setViewCount(news.getViewCount() == null ? 1 : news.getViewCount() + 1);
            boolean updated = newsService.updateById(news);
            return Result.success(updated);

        }else{
            return Result.fail("新闻不存在");
        }

    }
}